import sys
import ast
import webbrowser
# import console
import html5lib
import requests
from html5lib import parse

# 请求头部
headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7",
    "Origin": "http://yiqing.ctgu.edu.cn",
    "Referer": "http://yiqing.ctgu.edu.cn/wx/index/login.do"
}

loginUrl = 'http://yiqing.ctgu.edu.cn/wx/index/loginSubmit.do'
toApplyUrl = 'http://yiqing.ctgu.edu.cn/wx/health/toApply.do'
saveUrl = 'http://yiqing.ctgu.edu.cn/wx/health/saveApply.do'


def crawl(u):
    session = requests.session()

    params = {'username': u[0], 'password': u[1]}

    res = session.post(loginUrl, params, headers=headers)
    if res.ok:
        # 获取报平安的html
        toApplyRes = session.get(toApplyUrl, headers=headers)
        if toApplyRes.ok:
            doc = parse(toApplyRes.content.decode("utf-8"))


            # print(doc)
            content = doc.findall("div")

            et('打卡成功')
        else:
            et('打卡失败')

        # saveParams = {
        #     'province': '湖北省',
        #     'city': '宜昌市',
        #     'district': '西陵区',
        #     'adcode': 420502,
        #     'longitude': 111,
        #     'latitude': 31,
        #     'sfqz': '否',
        #     'sfys': '否',
        #     'sfzy': '否',
        #     'sfgl': '否',
        #     'status': 1,
        #     'szdz': '湖北省 宜昌市 西陵区',
        #     'sjh': 18871713930,
        #     'lxrxm': '',
        #     'lxrsjh': '',
        #     'sffr': '否',
        #     'sffrAm': '否',
        #     'sffrNoon': '否',
        #     'sffrPm': '否',
        #     'sffy': '否',
        #     'sfgr': '否',
        #     'qzglsj': '',
        #     'qzgldd': '',
        #     'glyy': '',
        #     'mqzz': '',
        #     'sffx': '否',
        #     'qt': ''
        # }
        # saveRes = session.post(saveUrl, saveParams, headers=headers)
        # if saveRes.ok:
        #     print(saveRes)
        #     et('打卡成功')
        # else:
        #     et('打卡失败')
    else:
        et('打卡失败')


'''
获取ios捷近传递参数

'''


def get_url():
    rawInput = sys.argv.__str__()
    listInput = ast.literal_eval(rawInput)
    return [listInput[1], listInput[2]]


'''
    封装提示
'''


def alert(text):
    print(text)
    # console.alert(text, '', '确定', hide_cancel_button=True)


'''
    退出函数
'''


def et(u):
    # clipboard.set(resText')
    # webbrowser.open('workflow://')  # 返回 Workflow 应用
    print(u)


'''
    入口函数
'''
if __name__ == '__main__':
    try:
        # data = ['202004010021001', '254444']
        data = ['202004010021027', '025320']
        crawl(data)
    except:
        et('打卡失败')
